package l1j.server.server.command.executor;

import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.Config;
import l1j.server.server.datatables.ItemTable;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_SystemMessage;
import l1j.server.server.templates.L1Item;

public class L1Prize implements L1CommandExecutor {
	private static Logger _log = Logger.getLogger(L1Prize.class.getName());

	public static L1CommandExecutor getInstance() {
		return new L1Prize();
	}
	private L1Prize() {
	}

	@Override
	public void execute(L1PcInstance pc, String cmdName, String arg) {

		try {
			final StringTokenizer st = new StringTokenizer(arg);
			final String nameid = st.nextToken();
			int count = 1;
			if (st.hasMoreTokens()) {
				count = Integer.parseInt(st.nextToken());
			}
			int itemid = 0;
			try {
				itemid = Integer.parseInt(nameid);
			} catch (final NumberFormatException e) {
				itemid = ItemTable.getInstance().findItemIdByNameWithoutSpace(nameid);
				if (itemid == 0) {
					pc.sendPackets(new S_SystemMessage("找不到符合條件項目。"));
					return;
				}
			}
			final L1Item temp = ItemTable.getInstance().getTemplate(itemid);
			if (temp != null) {
				Config.活動獎品[0] = itemid;
				Config.活動獎品[1] = count;
				pc.sendPackets(new S_SystemMessage("活動獎品設定為:"+temp.getName()+" 數量:"+ count));
			} else {
				pc.sendPackets(new S_SystemMessage("指定的道具編號不存在"));
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
			pc.sendPackets(new S_SystemMessage(".活動獎品 itemid|name [數目]請輸入。"));
		}
	}
}
